package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.IApArticleService;
import com.heima.article.service.IHotArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import sun.awt.image.PixelConverter;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class HotArticleServiceImpl implements IHotArticleService {

    @Autowired
    IApArticleService apArticleService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void compute() {

        Date now = new Date();

        Date to = new Date(now.getYear(), now.getMonth(), now.getDate());

        Date from = new Date(to.getTime() - 5 * 24 * 60 * 60 * 1000);

        LambdaQueryWrapper<ApArticle> query = new LambdaQueryWrapper<>();

        query.ge(ApArticle::getPublishTime, from);

        query.le(ApArticle::getPublishTime, to);

        List<ApArticle> list = apArticleService.list(query);

        for (ApArticle article : list) {

            double score = computeScore(article);

            ApArticle articleToCache = new ApArticle();

            articleToCache.setId(article.getId());

            articleToCache.setTitle(article.getTitle());

            articleToCache.setAuthorId(article.getAuthorId());

            articleToCache.setAuthorName(article.getAuthorName());

            articleToCache.setImages(article.getImages());

            articleToCache.setChannelId(article.getChannelId());

            articleToCache.setChannelName(article.getChannelName());

            articleToCache.setPublishTime(article.getPublishTime());

            if (score > 0) {

                String firstKey = "hot_article_first_page_0";

                String json = JSON.toJSONString(articleToCache);

                redisTemplate.opsForZSet().add(firstKey, json, score);

                redisTemplate.expire(firstKey, 23, TimeUnit.HOURS);

                String channelKey = "hot_article_first_page_" + article.getChannelId();

                redisTemplate.opsForZSet().add(channelKey, json, score);

                redisTemplate.expire(channelKey, 23, TimeUnit.HOURS);
            }
        }
    }

    private double computeScore(ApArticle article) {

        double score = 0;

        if (article.getViews() != null) {

            score += article.getViews() * 1;
        }
        if (article.getLikes() != null) {

            score += article.getLikes() * 3;
        }
        if (article.getIsComment() != null) {

            score += article.getComment() * 5;
        }
        if (article.getCollection() != null) {
            score += article.getCollection() * 8;
        }
        return score;
    }
}
